package com.tjp.eight.views.refresh;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;

import com.tjp.eight.R;
import com.tjp.eight.utils.BitmapUtils;

/**
 * Created by Administrator on 2017/3/20.
 */

public class MyRefreshDrawable extends RefreshDrawable implements Runnable {

    private Paint mPaint;
    private boolean isRunning = false;
    private Rect bound;
    private int Type = 0;
    private Handler mHandler = new Handler();
    private int offset = 0;

    public MyRefreshDrawable(Context context, PullRefreshLayout layout) {
        super(context, layout);
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }

    @Override
    public void setPercent(float percent) {
        invalidateSelf();
    }

    @Override
    public void setColorSchemeColors(int[] colorSchemeColors) {

    }

    @Override
    public void offsetTopAndBottom(int offset) {
        this.offset += offset;
        invalidateSelf();
    }

    @Override
    public void start() {
        isRunning = true;
        mHandler.postDelayed(this, 20);
        invalidateSelf();
    }

    @Override
    public void stop() {
        mHandler.removeCallbacks(this);
        isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        bound = bounds;
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.save();
        drawPic(canvas);
        canvas.restore();
        if (isRunning) {
            invalidateSelf();
        }
    }

    private void drawPic(Canvas canvas) {
        int aH = BitmapUtils.getBitmap(getContext(), R.drawable.refresh_bg).getWidth() / 2;
        int aW = BitmapUtils.getBitmap(getContext(), R.drawable.refresh_bg).getHeight();
        int bH = BitmapUtils.getBitmap(getContext(), R.drawable.refresh_04).getWidth() / 2;
        int bW = BitmapUtils.getBitmap(getContext(), R.drawable.refresh_04).getHeight();

        int offset = this.offset - aW;
        int boffset = this.offset - aW;
        canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_bg), bound.width() / 2 - aH, offset, mPaint);
        switch (Type) {
            case 0:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_04), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 1:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_07), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 2:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_10), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 3:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_13), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 4:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_16), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 5:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_19), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 6:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_22), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 7:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_25), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 8:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_28), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 9:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_31), bound.width() / 2 - bH, boffset, mPaint);
                break;
            case 10:
                canvas.drawBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.refresh_37), bound.width() / 2 - bH, boffset, mPaint);
                break;
        }


    }

    @Override
    public void run() {
        if (isRunning) {
            Type++;
            if (Type > 10) Type = 0;
            mHandler.postDelayed(this, 20);
            invalidateSelf();
        }
    }
}
